<!DOCTYPE html>
<html>
<head>
    <title>this test</title>
    <script type="text/javascript" src="../test-jq/js/jquery-1.8.3.min.js"></script>
</head>

<body>


<script type="text/javascript">

	/*The Core Primitive 核心原语*/
	function hello(thing) {
	  console.log(this + " says hello " + thing);
	}

	hello.call("Yehuda", "world"); //=> Yehuda says hello world
	// hello(", I'm window"); //[object Window] says hello , I'm window 
	// hello.call(window, ", I'm window")//[object Window] says hello , I'm window 
</script>




<script type="text/javascript">
	/*Simple Function Invocation 简单函数调用*/
	function hello(thing) {
	  console.log("Hello " + thing);
	}
	// this:
	hello("world");	//Hello world 	
	// desugars to:
	hello.call(window, "world");  
	
</script>


<script type="text/javascript">
	/*Member Functions 成员函数*/
	var person = {
	  name: "Brendan Eich",
	  hello: function(thing) {
	    console.log(this + " says hello " + thing);
	  }
	}
	// this:
	person.hello("world"); //[object Object] says hello world 
	// desugars to this:
	person.hello.call(person, "world");

</script>




<script type="text/javascript">

	function hello(thing){
	    console.log(this + " says hello " + thing);
	}
	person = {name : "Brendan Eich"}
	person.hello = hello;

	person.hello("world"); //[object Object] says hello world
	hello("world"); 	   //[object Window] says hello world 

</script>


<script type="text/javascript">
	/*使用Function.prototype.bind*/
	var person = {
		name : "Brendan Eich",
		hello : function(thing){
			console.log(this.name + " says hello " + thing);
		}
	}

	var boundHello = function(thing){
		return person.hello.call(person, thing);
	}

	boundHello("world");  //Brendan Eich says hello world


</script>


<a href="#" id="some-div">xxxx</a>
<script type="text/javascript">
var __bind = function(func, thisValue) {  
	return function() {  
		return func.apply(thisValue, arguments);  
	}  
}  

var person = {  
	name: "Alex Russell",  
	hello: function() { console.log(this.name + " says hello world"); }  
}  


// $("#some-div").click(person.hello.bind(person));  
$("#some-div").click(__bind(person.hello, person));  

</script>



<script>


/*情况一：纯粹的函数调用*/
/*var x = 1;
function test(){
	console.log(this);  //window 证明this就是全局对象
	console.log(this.x);  //1
	this.x = 0;
	console.log(this.x);  //0
}
console.log(this.x); //1
test();
console.log(this.x); //0*/





/*情况二：作为对象方法的调用*/
/*function test(){
	console.log(this.x);  //this指函数的调用者
}
var o = {};
o.x = 1;
o.m = test;
o.m(); //1*/






/*情况三 作为构造函数调用*/

/*var x = 2;
function test(){
	this.x = 1;
}
var o = new test();   //没有改变global的x
console.log(o.x); //1
console.log(x);	//2

test();  //改变了global的x
console.log(o.x); //1
console.log(x);	//1
*/





/*apply调用*/
/*var x = 2;
function test(){
	console.log(this.x);
}

var o = {};
o.x = 1;
o.m = test;

o.m();  //1
o.m.apply(); //2  参数为空时，默认调用全局对象
o.m.apply(o);  //1*/






/*var Name = "global"
function OuterFoo(){
	var Name = "local";
    this.Name = 'this'; 
    function InnerFoo(){
       // var Name = 'private'; 
       console.log(Name + ', ' + this.Name);  
    }
    return InnerFoo;
}
OuterFoo()();//local, this  InnerFoo的调用者是OuterFoo，所以this.name是this
*/





/*var Name = "global"
function OuterFoo(){

	var Name = "local"
    this.Name = 'this'; 

    function InnerFoo(){
     	// var Name = 'private'; 
     	console.log(Name + ', ' + this.Name);  
    }

    this.show1 = function(){
    	console.log(Name + ', ' + this.Name);
    }

    this.show2 = function(){
    	InnerFoo();
    }

}

var of = new OuterFoo();
of.show1(); //local, this
of.show2(); //local, global  InnerFoo的调用者是window，所以this.name是global
*/









/*var point = { 
	x : 0, 
	y : 0, 
	moveTo : function(x, y) { 
		// 内部函数
		var moveX = function(x) { 
			this.x = x; 	//this 绑定到了哪里？window! 创建了全局变量x
		}; 
		// 内部函数
		var moveY = function(y) { 
			this.y = y; 	//this 绑定到了哪里？
		}; 

		moveX(x); 
		moveY(y); 
	} 
}; 
console.log(window.x);	//undefined
console.log(window.y);	//undefined
point.moveTo(1, 1);  
console.log(point.x); //==>0 
console.log(point.y); //==>0 
console.log(x); //==>1 
console.log(y); //==>1
console.log(window.x);//==>1 
console.log(window.x);//==>1 */






/*var point = { 
	x : 0, 
	y : 0, 
	moveTo : function(x, y) { 
		var that = this;   
		// 内部函数
		var moveX = function(x) { 
			that.x = x; 
		}; 
		// 内部函数
		var moveY = function(y) { 
			that.y = y; 
		} 
		moveX(x); 
		moveY(y); 
	} 
}; 
// point.moveTo(1, 1);  //moveTo通过point.来调用，this指向moveTo的调用者point
// console.log(point.x); //==>1 
// console.log(point.y); //==>1
// // console.log(x); //Uncaught ReferenceError: x is not defined 

var fn = point.moveTo;
fn(10,10); 	//这里的moveTo是由fn来调用的，fn是window对象的一个属性，that保存的是widow对象
console.log(point.x); //==>0
console.log(point.y); //==>0
//坑爹的又弄出了全局变量~~~
console.log(x); //==>10 
console.log(y); //==>10
console.log(window.x);//==>10 
console.log(window.x);//==>10 */








/*
function JSClass()
{
  var myName = 'jsclass';
  this.m_Name = 'JSClass';
}

JSClass.prototype.ToString = function()
{
  alert(myName + ', ' + this.m_Name);
};

var jc = new JSClass();
jc.ToString();  //Uncaught ReferenceError: myName is not defined 
*/

</script>
</body>
</html>